package lc.smart.android.main;

import java.io.IOException;

import lc.smart.android.util.ImagesUtil;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class MediaStoreGalleryActivity extends Activity
{
	public static final String TAG_STRING = "MediaStoreGalleryActivity";//标识
	private Context mContext;
	public final static int DISPLAYWIDTH = 400;
	public final static int DISPLAYHEIGHT = 400;

	TextView titleTextView;
	ImageButton imageButton;

	Cursor cursor;
	Bitmap bmp;
	String imageFilePathString;
	int fileColumn;
	int titleColum;
	int displayColumn;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mContext = this;

		setContentView(R.layout.activity_media_store_gallery);

		titleTextView = (TextView) findViewById(R.id.TitleTextView);
		imageButton = (ImageButton) findViewById(R.id.ImageButton);

		String[] columnStrings = { Media.DATA, Media._ID, Media.TITLE,
				Images.Media.DISPLAY_NAME };
		cursor = managedQuery(Images.Media.EXTERNAL_CONTENT_URI, columnStrings,
				null, null, null);
		fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		titleColum = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE);
		displayColumn = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
		if (cursor.moveToFirst())
		{
			titleTextView.setText(cursor.getString(displayColumn));
			imageFilePathString = cursor.getString(fileColumn);
			getEXIF(imageFilePathString);
			bmp = ImagesUtil.getBitmap(imageFilePathString, DISPLAYWIDTH,
					DISPLAYHEIGHT);

			imageButton.setImageBitmap(bmp);
		}
		imageButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (cursor.moveToNext())
				{
					titleTextView.setText(cursor.getString(displayColumn));
					imageFilePathString = cursor.getString(fileColumn);
					getEXIF(imageFilePathString);
					bmp = ImagesUtil.getBitmap(imageFilePathString,
							DISPLAYWIDTH, DISPLAYHEIGHT);

					imageButton.setImageBitmap(bmp);
				}

			}
		});

	}

	/**
	 * 内部源数据展示
	 * @param imageFilePathString 文件路径
	 * */
	public void getEXIF(String imageFilePathString)
	{
		if (SmartApplication.is_DubugMode)
		{
			Log.v(TAG_STRING, "getEXIF called!");
		}
		ExifInterface exifInterface;
		try
		{
			exifInterface = new ExifInterface(imageFilePathString);
			String imageDescriptionString = exifInterface
					.getAttribute("ImageDescription");
			if (imageDescriptionString != null && SmartApplication.is_DubugMode)
			{
				Log.i(TAG_STRING, imageDescriptionString);
			}
			else
			{
				//使用exifInterface保存exif数据到图像文件中
				exifInterface.setAttribute("ImageDescription", "Something New");
			}

		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.media_store_gallery, menu);
		return true;
	}

}
